package com.hack4jp.hackearthquakes;

import android.content.Context;
import android.content.SharedPreferences;

public class Preferences {

	public final static String 
		OAUTH_ACCESS_TOKEN = "OAuthAccessToken",
		OAUTH_ACCESS_TOKEN_SECRET = "OAuthAccessTokenSecret";
	
	private static Context _ctx;	
	private static Preferences _instance = null;

	public static void setContext(Context ctx) {
		_ctx = ctx;
	}
	
	public static Preferences getInstance() throws Exception {
		if (_instance == null) {
			_instance = new Preferences();
		}
		
		return _instance;
	}	
	

	private SharedPreferences _pref;
	
	private Preferences() throws Exception {
		if (_ctx == null)
			throw new Exception("context is null");
			
		_pref = _ctx.getSharedPreferences("HackEarthquake", Context.MODE_PRIVATE);
	}	  
	
	public void write(String key, String value) {		
		SharedPreferences.Editor editor = _pref.edit();
		editor.putString(key, value);
		editor.commit();
	}
	
	public String read(String key) {
		return _pref.getString(key, "");
	}
	
	public void Remove(String key) {
		SharedPreferences.Editor editor = _pref.edit();
		editor.remove(key);
		editor.commit();
	}
	
	public void Clear() {
		SharedPreferences.Editor editor = _pref.edit();
		editor.clear();
		editor.commit();
	}
}
